package cn.hd01.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.domain.Specifications;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import cn.hd01.repository.CustomerOperationRepository;
import cn.hd01.repository.entity.COrder;
import cn.hd01.repository.entity.Customer;
import cn.hd01.repository.entity.CustomerOperation;
import cn.hd01.repository.entity.DistributorDetails;
import cn.hd01.service.COrderService;
import cn.hd01.service.CustomerOperationService;
import cn.hd01.service.CustomerService;
import cn.hd01.service.DistributorDetailsService;

@Service
public class CustomerOperationServiceImpl extends BaseServiceImpl<CustomerOperation, Integer> implements CustomerOperationService {

	private CustomerOperationRepository repository;

	@Resource
	private COrderService cOrderService;
	
	@Resource
	private CustomerService customerService;
	
	@Resource
	private DistributorDetailsService distributorDetailsService;
	
	@Autowired
	public CustomerOperationServiceImpl(CustomerOperationRepository repository) {
		super(repository);
		this.repository = repository;
	}

	@Override
	public Page<CustomerOperation> findAll(final CustomerOperation operation, Pageable pageable) {
		if (operation == null) {
			return repository.findAll(pageable);
		}

		Page<CustomerOperation> result =  repository.findAll(Specifications.where(new Specification<CustomerOperation>() {
			@Override
			public Predicate toPredicate(Root<CustomerOperation> r, CriteriaQuery<?> q, CriteriaBuilder cb) {
				List<Predicate> predicates = new ArrayList<Predicate>();
				
				if(operation.getOrderId() != null){
					predicates.add(cb.equal(r.get("orderId").as(String.class), operation.getOrderId()));
				}
				
				if(StringUtils.hasText(operation.getCustomerName())){
					predicates.add(cb.like(r.get("customerName").as(String.class), "%" + operation.getCustomerName() + "%"));
				}
				
				if(StringUtils.hasText(operation.getOperator())){
					predicates.add(cb.like(r.get("operator").as(String.class), "%" + operation.getOperator() + "%"));
				}
				
				if(StringUtils.hasText(operation.getStatus())){
					predicates.add(cb.equal(r.get("status").as(String.class), operation.getStatus()));
				}
				
				if(StringUtils.hasText(operation.getCustomerName())){
					predicates.add(cb.like(r.get("customerName").as(String.class),  "%" +  operation.getCustomerName() + "%"));
				}
				/*if(StringUtils.hasText(operation.getProductId()) && !operation.getProductId().equals("-1")){
					predicates.add(cb.like(r.get("productId").as(String.class), "%" + operation.getProductId() + ",%"));
				}*/
				
				if(StringUtils.hasText(operation.getProductName()) && !operation.getProductName().equals("-1")){
					predicates.add(cb.like(r.get("productName").as(String.class), "%" + operation.getProductName() + "%"));
				}
				
				if(operation.getOperationTime() != null){
					predicates.add(cb.equal(r.get("operationTime").as(Date.class), operation.getOperationTime()));
				}
				
				if(operation.getOperationBeginTime() != null){
					predicates.add(cb.greaterThanOrEqualTo(r.get("operationTime").as(Date.class), operation.getOperationBeginTime()));
				}
				
				if(operation.getOperationEndTime() != null){
					predicates.add(cb.lessThanOrEqualTo(r.get("operationTime").as(Date.class), operation.getOperationEndTime()));
				}
				
				return cb.and(predicates.toArray(new Predicate[] {}));
			}
		}), pageable);
		
		
		List<CustomerOperation> coList = result.getContent();
		if(!CollectionUtils.isEmpty(coList)){
			for(CustomerOperation co : coList){
				Integer orderId = co.getOrderId();
				if(orderId != null){
					COrder corder = cOrderService.findOne(orderId);
					String distributorName = corder.getDistributorName();
					DistributorDetails details = distributorDetailsService.findByDistributorId(corder.getDistributorId());
					if(details != null){
						co.setDistributorName(details.getShopName() + "(" + distributorName + ")");
					}else{
						co.setDistributorName(distributorName);
					}
				} else{
					Customer c = customerService.findOne(co.getCustomerId());
					if(c != null && c.getRecommendDistributorId() != null){
						Customer d = customerService.findOne(c.getRecommendDistributorId());
						if(d != null){
							DistributorDetails details = distributorDetailsService.findByDistributorId(d.getId());
							if(details != null && StringUtils.hasText(details.getShopName())){
								co.setDistributorName(details.getShopName() + "(" + d.getName() + ")");
							}else{
								co.setDistributorName(d.getName());
							}
						}else{
							co.setDistributorName(d.getName());
						}
					}
					
				}
			}
		}
		
		return result;
	}
}
